// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import GameWS from "./GameWS";
import FireBaseMgr from "./FireBaseMgr";
import LanguageMng from "./LanguageMng";
import LanguageMngTs from "./LanguageMngTs";

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    ctor(){

    },

    async goLobby (token) {
        let baseUrl = cc.gg.httpUtil.rootUrl
        FireBaseMgr.sendSignUp()
        cc.gg.gameDataUtil.goToLobby(true)
        return GameWS.connect(`${baseUrl}/pvp/battle`)
            .then(() => {
                return GameWS.login(token)
            })
            .then(code => {
                if (code == 0) {
                    console.log("Connect to signalr success: ", code);
                    // FireBaseMgr.sendSignUp()
                    // cc.gg.gameDataUtil.goToLobby(true)
                } else {
                    console.log("Connect to signalr failed: ", code);
                }
            }).catch(err => {
                console.log("Failed to connecting with signalR:", err.toString());
            });
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    // start () {
    //
    // },

    // update (dt) {},

    showNoticeReconnect:function (cb) {
        console.log("Show notice reconnect!")
        let title = LanguageMng.getSentence(3)
        let btnStr = LanguageMng.getSentence(57)
        let contentStr = LanguageMngTs.getSentence(385)//"Disconnected with server,please check your web and reconnect."
        let cba = async function () {
            cc.gg.toastMgr.removeDialog()
            await cc.gg.toastMgr.showNetWaitting()
            let baseUrl = cc.gg.httpUtil.rootUrl
            let token = cc.gg.httpUtil.USER_TOKEN
            await GameWS.connect(`${baseUrl}/pvp/battle`)
                .then(() => {
                    return GameWS.login(token)
                })
                .then(code => {
                    if (code == 0) {
                        console.log("Connect to signalr success: ", code);
                        FireBaseMgr.sendSignUp()
                        cc.gg.gameDataUtil.goToLobby(true)
                    } else {
                        console.log("Connect to signalr failed: ", code);
                        cc.gg.gameCtl.showNoticeReconnect(cb)
                    }
                }).catch(err => {
                    cc.gg.gameCtl.showNoticeReconnect(cb)
                    console.log("Failed to connecting with signalR:", err);
                });
            cc.gg.toastMgr.removeNetWaitting()
            // cc.gg.gameDataUtil.goToLobby(true)
        }.bind(this)
        cc.gg.toastMgr.showDialogOneButton(title,btnStr,contentStr,cba)
    }
});
